from django.db import models
from public.models import User
# Create your models here.


class Permissions(models.Model):
    class Meta:
        verbose_name_plural = '权限管理'
        permissions = [
            ('Scheduler_view', '定时任务-查看权限'),
            ('Scheduler_manage', '定时任务-任务管理权限'),
        ]


class SchedulerTpye(models.Model):
    Name = models.CharField(max_length=80, unique=True)
    Shlrmethod = models.CharField(max_length=80, unique=True)
    Argslist = models.TextField(blank=True)
    Remark = models.TextField(blank=True)

    class Meta:
        verbose_name_plural = '定时任务类型'

    def __str__(self):
        return self.Name


class PromResult(models.Model):
    ip = models.CharField(max_length=80, blank=False, null=False)
    fetchdate = models.CharField(max_length=40, null=False, blank=False)
    cpunum = models.FloatField(null=True)
    memnum = models.FloatField(null=True)
    cpumax = models.FloatField(null=True)
    cpuavg = models.FloatField(null=True)
    memusage = models.FloatField(null=True)
    diskusage = models.FloatField(null=True)
    ioread = models.FloatField(null=True)
    iowrite = models.FloatField(null=True)
    hostname = models.CharField(max_length=200, null=True)
    osname = models.CharField(max_length=200, null=True)
    hosttype = models.CharField(max_length=200, null=True)
    interval = models.CharField(max_length=40, null=True, blank=False)

    class Meta:
        constraints = [models.UniqueConstraint(fields=['ip', 'fetchdate', 'interval'], name='unique_promResult'), ]
        verbose_name_plural = '资源监控'

    def __str__(self):
        return self.ip


class SchedulerJob(models.Model):
    JobName = models.CharField(max_length=80, unique=True)
    Jobtpye = models.ForeignKey(SchedulerTpye, null=True, on_delete=models.SET_NULL)
    Jobargs = models.TextField(null=True, blank=True)
    Jobowner = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
    Jobstatus = models.IntegerField(default=0)
    reservation = models.CharField(max_length=80, null=True)
    starttime = models.DateTimeField()
    endtime = models.DateTimeField()
    plan = models.IntegerField(default=1)
    intervals = models.IntegerField(null=True)
    Remark = models.TextField(null=True, blank=True)
    plantime = models.DateTimeField(null=True)
    crontime = models.CharField(max_length=80, null=True)

    class Meta:
        verbose_name_plural = '定时任务'
        ordering = ['JobName']

    def __str__(self):
        return self.JobName
